﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Modbus.Device;

namespace APPWG4806
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        void GetChangeRate() 
        {
            string url = "http://op.juhe.cn/onebox/exchange/currency";
            string apiKey = "4e482f1022a529b2fb265818b4f2982d";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("key", apiKey);
            dic.Add("from", "EUR");
            dic.Add("to", "CNY");
            dic.Add("version", "2");

            string strUrl = url + "?" + string.Join("&", dic.Select(x => x.Key + "=" + x.Value));

            using (WebClient client = new WebClient())
            {
                client.Encoding = Encoding.UTF8;
                string data = client.DownloadString(strUrl);
                MessageBox.Show(data);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        void Test() 
        {
            try
            {
               
                
                //主站
                ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serialPort);
                //从站地址
                byte slaveId = 1;
                //寄存器地址
                ushort registerAddress = 4;
                //写入的值
                ushort writeValue = 0;
                master.WriteSingleRegister(slaveId, registerAddress, writeValue);
                serialPort.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        SerialPort serialPort = new SerialPort("COM2");
        private void Form4_Load(object sender, EventArgs e)
        {
            serialPort.BaudRate = 9600;
            serialPort.Parity = Parity.None;
            serialPort.DataBits = 8;
            serialPort.StopBits = StopBits.One;
            serialPort.Open();
        }
    }
}
